#include "node_extapp.h"
#include "serialization/asn1.h"
//	#include "hid_log.h"
#include "t1.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "Debug.h"
#include "Log.h"
//	#include "nrf_log.h"

#define SamResponseGetContentElement2 (0x00)

typedef enum { extappModuleCommandSamResponse2, TOTAL_COMMANDS } extappModuleCommandId_t;

typedef t1_inf_t (*command_handler)(asn1_tlv_t cmd);

typedef struct {
    extappModuleCommandId_t extappModuleCommandId;
    command_handler handler;
} extappModuleCommands_t;

static t1_inf_t node_extapp_samResponseGetContentElement2(asn1_tlv_t cmd) {
    asn1_tlv_t contentElementData = asn1_unwrap(cmd.value);
    LOG_BUFS("ContentElementData: ", contentElementData.value, contentElementData.len);

//	    app_notify_pacs_recv();
	t1_inf_t inf = {0};
	return inf;
}

static t1_inf_t node_extapp_samResponse2(asn1_tlv_t cmd) {
    asn1_tlv_t samResponse2innerTag = asn1_unwrap(cmd.value);
    if (samResponse2innerTag.tag.tagid == SamResponseGetContentElement2) {
        node_extapp_samResponseGetContentElement2(samResponse2innerTag);
    }
	t1_inf_t inf = {0};
	return inf;
}

static extappModuleCommands_t const extappModuleCommands[] = {
    [extappModuleCommandSamResponse2] = {0x13, node_extapp_samResponse2}};

void node_extapp_handler(t1_inf_t *inf, /*t1_len_t*/ uint16_t len) {
    asn1_tlv_t payload = asn1_unwrap(inf->payload);

    if (payload.tag.tagid == (ASN1_Payload_t)response) {
        asn1_tlv_t extappCommand = asn1_unwrap(payload.value);
        for (extappModuleCommandId_t i = 0; i < TOTAL_COMMANDS; i++) {
            if (extappModuleCommands[i].extappModuleCommandId == extappCommand.tag.tagid) {
                extappModuleCommands[i].handler(extappCommand);
                return;
            }
        }
        LOG_I("No handler for command %02X\n", extappCommand.tag.tagid);
    }
    LOG_I("Unknown payload tagid %02X\n", payload.tag.tagid);
}